<template>
  <div id="home">
    <el-row>
      <el-col :span="8" style="padding-right: 10px">
        <!-- 用户信息卡片 -->
        <el-card class="user-card" shadow="hover">
          <div class="user-head">
            <img src="../assets/images/user.jpg" alt="" />
            <div class="user-info">
              <p class="user-name">Admin</p>
              <p class="user-access">超级管理员</p>
            </div>
          </div>
          <div class="login-info">
            <p>上次登录时间：<span>2021-7-31</span></p>
            <p>上次登录地点：<span>深圳</span></p>
          </div>
        </el-card>
        <!-- 数据表格卡片 -->
        <el-card class="table-card" shadow="hover">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column
              v-for="(value, key) of tableLabel"
              :key="key"
              :prop="key"
              :label="value"
            ></el-table-column>
          </el-table>
        </el-card>
      </el-col>
      <el-col class="number-show" :span="16" style="padding-left: 10px">
        <!-- 订单数据卡片 -->
        <div class="order">
          <el-card
            class="order-card"
            v-for="item of orderData"
            :key="item.name"
            :body-style="{ display: 'flex',padding: 0 }"
          >
            <i
              class="icon"
              :class="`el-icon-${item.icon}`"
              :style="{ backgroundColor: item.color }"
            ></i>
            <div class="detail">
              <p class="price">{{ "￥" + item.value }}</p>
              <p class="order-name">{{ item.name }}</p>
            </div>
          </el-card>
        </div>
        <!-- 折线图卡片 -->
        <el-card class="line-chart">
          <!-- 折线图 -->
          <!-- 为 ECharts 准备一个定义了高宽的 DOM 容器 -->
          <div ref="lineEchart" class="lineEchart"></div>
        </el-card>
        <!-- 柱状图和饼图卡片 -->
        <div class="charts">
          <!-- 柱状图 -->
          <el-card class="bar-chart">
            <div ref="barEchart" class="barEchart"></div>
          </el-card>
          <!-- 饼图 -->
          <el-card class="pie-chart">
            <div ref="pieEchart" class="pieEchart"></div>
          </el-card>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
// 导入数据
import { getData } from "../api";
// 引入echarts
import * as echarts from 'echarts';

export default {
  name: "Home",
  data() {
    return {
      tableLabel: {
        name: "课程",
        todayBuy: "今日购买",
        monthBuy: "本月购买",
        totalBuy: "总购买",
      },
      tableData: [],
      orderData: [
        {
          name: "今日支付订单",
          value: "1234",
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "今日收藏订单",
          value: "210",
          icon: "star-on",
          color: "#ffb980",
        },
        {
          name: "今日未支付订单",
          value: "1234",
          icon: "s-goods",
          color: "#5ab1ef",
        },
        {
          name: "本月支付订单",
          value: "1234",
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "本月收藏订单",
          value: "210",
          icon: "star-on",
          color: "#ffb980",
        },
        {
          name: "本月未支付订单",
          value: "1234",
          icon: "s-goods",
          color: "#5ab1ef",
        },
      ],
    };
  },
  mounted() {
    getData().then(({ data }) => {
      const { tableData } = data.data;
      // 获取表格卡片的数据
      this.tableData = tableData;

      // 折线图
      //基于准备好的dom - this.$refs.lineEchart, 通过 echarts.init(dom)，初始化echarts实例
      const lineEchart = echarts.init(this.$refs.lineEchart);
      // 指定图标的配置项和数据 - option
      let lineEchartOption = {};
      // 获取数据
      const { orderData, userData, videoData } = data.data;
      // Object.keys() - 返回对象的枚举
      const xAxis = Object.keys(orderData.data[0]);
      const xAxisData = {
        data: xAxis,
      }
      // 获取图例 - legend
      lineEchartOption.legend = xAxisData;
      // lineEchartOption.legend = {
      //   data: orderData.date,
      // };
      // console.log(orderData.date);
      // 获取x轴名称 - xAxis
      lineEchartOption.xAxis = xAxisData;
      // 获取y轴 - yAxis
      lineEchartOption.yAxis = {};
      // 获取series
      lineEchartOption.series = [];
      // 分别获取需要显示在图表上的内容(注意, 有多个)
      // 1. name: 'xAxis[0~5]'
      // 2. type: 'line' //表示折线图
      // 3. data: []
      // console.log(xAxis);
      console.log(orderData);
      // 遍历xAxis
      xAxis.forEach(key => {
        lineEchartOption.series.push({
          // name: orderData.date,
          name: key,
          type: 'line',
          data: orderData.data.map((item) => {
            return item[key];
          }),
        })
      });
      // console.log(lineEchartOption.series);
      // 根据配置显示图表 - 调用setOption(echarts实例)方法
      // console.log(lineEchartOption);
      lineEchart.setOption(lineEchartOption);

      // 柱状图
      const barEchart = echarts.init(this.$refs.barEchart);
      let barEchartOption = {
        legend: {
          // 图例文字颜色
          textStyle: {
            color: '#333',
          },
        },
        grid: {
          left: '20%',
        },
        // 提示框
        tooltip: {
          trigger: 'axis',
        },
        xAxis: {
          type: 'category',
          data: userData.map(item => item.date),
          axisLine: {
            lineStyle: {
              color: '#17b3a3',
            },
          },
          axisLabel: {
            interval: 0,
            color: '#333',
          },
        },
        yAxis: [
          {
            type: 'value',
            axisLine: {
              lineStyle: {
                color: '#17b3a3',
              },
            },
          },
        ],
        color: ['#2ec7c9', '#b6a2de'],
        series: [
          {
            name: '新增用户',
            data: userData.map(item => item.new),
            type: 'bar',
          },
          {
            name: '活跃用户',
            data: userData.map(item => item.active),
            type: 'bar',
          },
        ]
      };
      // 根据配置显示图表
      barEchart.setOption(barEchartOption);

      // 饼图
      let pieEchart = echarts.init(this.$refs.pieEchart);
      const pieEchartOption = {
        // 提示框
        tooltip: {
          trigger: 'item',
        },
        color: [
          "#0f78f4",
          "#dd536b",
          "#9462e5",
          "#a6a6a6",
          "#e1bb22",
          "#39c362",
          "#3ed1cf",
        ],
        series: [
          {
            type: 'pie',
            data: videoData,
          }
        ],
      }
      pieEchart.setOption(pieEchartOption);
    });
  },
};
</script>

<style lang="less" scoped>
// 内边距清零
#home {
  .el-card {
    padding: 0;
  }
}

// 用户信息卡片
.user-head {
  display: flex;
  align-items: center;
  padding: 0 0 20px 0;
  margin-bottom: 20px;
  border-bottom: 1px solid #ccc;
  img {
    width: 150px;
    height: 150px;
    border-radius: 50%;
    margin-right: 40px;
  }
  .user-info {
    .user-name {
      font-size: 32px;
      margin-bottom: 10px;
    }
    .user-access {
      color: #999;
    }
  }
}
.login-info {
  p {
    line-height: 28px;
    font-size: 14px;
    color: #999;
    span {
      color: #666;
      margin-left: 60px;
    }
  }
}
// 表格数据卡片
.table-card {
  margin-top: 20px;
  height: 460px;
}
// 订单数据卡片
.order {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  .order-card {
    width: 32%;
    margin-bottom: 20px;
    .icon {
      width: 80px;
      height: 80px;
      font-size: 30px;
      text-align: center;
      line-height: 80px;
      color: #fff;
    }
    .detail {
      display: flex;
      // flex的轴为按列
      flex-direction: column;
      justify-content: center;
      margin-left: 15px;
      .price {
        font-size: 30px;
        margin-bottom: 10px;
        line-height: 30px;
      }
      .order-name {
        font-size: 14px;
        color: #999;
        text-align: center;
      }
    }
  }
}
// 折线图卡片
.line-chart {
  height: 280px;
  .lineEchart {
    height: 280px;
  }
}
// 柱状图和饼图
.charts {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
  .bar-chart, .pie-chart {
    width: 48%;
    height: 260px;
    .barEchart {
      height: 260px;
    }
    .pieEchart {
      height: 240px;
    }
  }
}
</style>
